<?php
namespace app\common\model;
use think\Model;
use think\Db;

class CommonModel extends Model
{
    /**
     * 统一处理分页数据
     * @param array data
     * @param int pagesize
     * @param int page
     */
    protected function pageData($data,$pagesize,$page,$count){
        return [
            'count'    => $count,
            'pagecount'=> ceil($count/$pagesize),
            'pagesize' => $pagesize,
            'page'     => $page,
            'data'     => $data
        ];
    }

    /**
     * 系统错误信息入库
     * @param int       uid
     * @param object    e
     * @param string    path
     */
    public static function savelog($uid,$e,$path)
    {
        $error = [
            'errorpath' => $path,
            'errorcode' => $e->getCode(),
            'errormsg'  => $e->getMessage(),
            'errorline' => $e->getLine(),
            'errorfile' => $e->getFile(),
            'uid'       => $uid,
        ];
        Db::table('error_log')->insert($error);
    }

    /**
     * 日志记录
     * @param string $userid
     * @param string $event
     * @param string $event_time
     * @param string $status
     * @param string $desc
     */
    public static function recordLog($userid,$event,$event_time,$status,$desc)
    {
        Db::table('log')->insert([
            'userid'        => $userid,
            'event'         => $event,
            'event_time'    => $event_time,
            'status'        => $status,
            'desc'          => $desc,
        ]);    
    }

    /**
     * 执行sql获取结果
     * @param string sql
     */
    protected function getRes($sql){
        return Db::query($sql);
    }

    /**
     * 执行sql获取数量
     * @param string sql
     */
    protected function getCount($sql){
        return Db::query($sql)[0]['count(*)'];
    }

    public static function write_active($userid)
    {
        Db::table('user')->where(['userid' => $userid])->update(['active_time' => date('Y-m-d H:i:s')]);    
    }
}